package com.itheima.tanhuauser.controller;


import com.itheima.Result;
import com.itheima.dto.ThFollowDto;
import com.itheima.pojo.ThFollow;
import com.itheima.pojo.User;
import com.itheima.tanhuauser.service.UserService;
import com.itheima.tanhuauser.thread.TokenThread;
import com.itheima.vo.UserVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/users")
@RefreshScope
public class UserController {
//    @Value("${server.port}")
//    private Integer port;
//
//    @GetMapping("/test")
//    public String test(){
//        System.out.println(port);
//        return port.toString();
//    }

    @Resource
    private UserService userService;

    /**
     * 发送短信验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public Result sendMsg(@RequestBody User user){
        boolean res=userService.sendMsg(user.getPhone());
        if (!res){
            return Result.error("验证码发送失败");
        }
        return Result.success("发送验证码成功");
    }

    /**
     * 用户登录
     * @param userVo
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserVo userVo){
        UserVo vo=userService.login(userVo);
        return Result.success(vo);
    }

    /**
     * 完善个人信息
     * @param userVo
     * @return
     */
    @PutMapping("/info")
    public Result updateMsg(@RequestBody UserVo userVo){
        boolean res=userService.updateMsg(userVo);
        if (!res){
            return Result.error("完善个人信息失败");
        }
        return Result.success();
    }

    @PostMapping("/like")
    public Result addLike(@RequestBody ThFollowDto thFollowDto){
        boolean res=userService.addLike(thFollowDto);
        if (!res){
            return Result.error("添加喜欢失败");
        }
        return Result.success();
    }

    @DeleteMapping("/like")
    public Result deleteLike(@RequestParam String followId){
        boolean res=userService.deleteLike(followId);
        if (!res){
            return Result.error("取消喜欢失败");
        }
        return Result.success();
    }

    @GetMapping("/like")
    public Result likeList(@RequestParam("type") Integer type,
                           @RequestParam("name") String name,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize){
        String id = TokenThread.get();
        List<User> list=userService.findLike(type,id,name,page,pageSize);
        return Result.success(list);
    }

}
